package com.shitou.shitouorder.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * class detail:
 *
 * @author 石头人
 */
@SuppressWarnings("all")
@Configuration
public class RabbitMQConfig {

    /**
     * 创建订单附属表
     */
    public final static String CREATE_ORDER_ITEM_QUEUE = "CREATE-ORDER-ITEM-QUEUE";
    /**
     * 交换机
     */
    public final static String SHITOU_ORDER_DLX_EXCHANGE = "SHITOU-ORDER-EXCHANGE";
    public final static String SHITOU_ORDER_UPDATE_EXCHANGE = "SHITOU-ORDER-UPDATE-EXCHANGE";
    public final static String SHITOU_AUTOCONFIRM_EXCHANGE = "SHITOU-AUTOCONFIRM-EXCHANGE";
    /**
     * 延时队列
     */
    public final static String SHITOU_TTL_QUEUE = "SHITOU-TTL-QUEUE";
    /**
     * 死信队列
     */
    public final static String SHITOU_CLOSE_QUEUE = "SHITOU-CLOSE-QUEUE";
    public final static String SHITOU_ORDER_ROUTING_KEY = "create.order.key";

    /**
     * 支付成功后，更新订单信息队列
     */
    public final static String UPDATE_ORDER_QUEUE = "UPDATE-ORDER-QUEUE";
    public final static String UPDATE_ORDER_KEY = "order.pay.success";
    /**
     * 订单回退
     */
    public final static String ORDER_REFOND_QUEUE = "ORDER-REFOND-QUEUE";

    /**
     * 自动收货延时消息队列
     * @return
     */
    public final static String ORDER_AUTOCONFIRM_TTL_QUEUE = "ORDER-AUTOCONFIRM-TTL-QUEUE";
    /**
     * 自动收货死信队列
     * @return
     */
    public final static String ORDER_AUTOCONFIRM_CLOSE_QUEUE = "ORDER-AUTOCONFIRM-CLOSE-QUEUE";
    public final static String ORDER_AUTOCONFIRM_KEY = "auto.confirm.key";


    /**
     * 退款成功后修改订单信息
     */
    public final static String REFUND_UPDATE_ORDER_INFO = "REFUND-UPDATE-ORDER-INFO";




    @Bean
    public Exchange exchange(){
        return new TopicExchange(SHITOU_ORDER_DLX_EXCHANGE, true, false,null);
    }
    @Bean
    public Exchange autoConfirmExchange(){
        return new TopicExchange(SHITOU_AUTOCONFIRM_EXCHANGE, true, false, null);
    }




    @Bean(REFUND_UPDATE_ORDER_INFO)
    public Queue refundUpdateOrderQueue(){
        return new Queue(REFUND_UPDATE_ORDER_INFO, true, false, false, null);
    }
    @Bean(ORDER_AUTOCONFIRM_TTL_QUEUE)
    public Queue autoConfirmQueue(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", SHITOU_AUTOCONFIRM_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", ORDER_AUTOCONFIRM_KEY);
        arguments.put("x-message-ttl", 1000 * 60 * 12); // 单位:毫秒 一小时仅仅用于测试，两天
        return new Queue(ORDER_AUTOCONFIRM_TTL_QUEUE, true, false, false, arguments);
    }
    @Bean(ORDER_AUTOCONFIRM_CLOSE_QUEUE)
    public Queue autoConfirmCloseQueue(){
        return new Queue(ORDER_AUTOCONFIRM_CLOSE_QUEUE, true, false, false, null);
    }
    @Bean(SHITOU_TTL_QUEUE)
    public Queue ttlQueue(){
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", SHITOU_ORDER_DLX_EXCHANGE);
        arguments.put("x-dead-letter-routing-key", SHITOU_ORDER_ROUTING_KEY);
        arguments.put("x-message-ttl", 1000 * 60 * 10); // 单位:毫秒 10分钟仅仅用于测试，实际根据需求，通常30分钟或者15分钟
        return new Queue(SHITOU_TTL_QUEUE, true, false, false, arguments);
    }
    @Bean(UPDATE_ORDER_QUEUE)
    public Queue updateOrderQueue(){
        return new Queue(UPDATE_ORDER_QUEUE, true, false, false , null);
    }
    @Bean(CREATE_ORDER_ITEM_QUEUE)
    public Queue createOrderItemQueue(){
        return new Queue(CREATE_ORDER_ITEM_QUEUE, true, false, false, null);
    }

    @Bean(SHITOU_CLOSE_QUEUE)
    public Queue closeQueue(){
        return new Queue(SHITOU_CLOSE_QUEUE, true, false, false, null);
    }
    @Bean(ORDER_REFOND_QUEUE)
    public Queue aliPayRefondQueue(){
        return new Queue(ORDER_REFOND_QUEUE, true, false, false, null);
    }






    @Bean
    public Binding TTLBinding(){
        return new Binding(SHITOU_TTL_QUEUE, Binding.DestinationType.QUEUE, SHITOU_ORDER_DLX_EXCHANGE, SHITOU_ORDER_ROUTING_KEY, null);
    }
    @Bean
    public Binding closeBinding(){
        return new Binding(SHITOU_CLOSE_QUEUE, Binding.DestinationType.QUEUE, SHITOU_ORDER_DLX_EXCHANGE, SHITOU_ORDER_ROUTING_KEY, null);
    }


    @Bean Binding autoConfirmTTLBinding(){
        return new Binding(ORDER_AUTOCONFIRM_TTL_QUEUE, Binding.DestinationType.QUEUE, SHITOU_AUTOCONFIRM_EXCHANGE, ORDER_AUTOCONFIRM_KEY, null);
    }
    @Bean Binding autoConfirmCloseBinding(){
        return new Binding(ORDER_AUTOCONFIRM_CLOSE_QUEUE, Binding.DestinationType.QUEUE, SHITOU_AUTOCONFIRM_EXCHANGE, ORDER_AUTOCONFIRM_KEY, null);
    }

}

